package com.venue.hjt.venueclass.ui;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.opencvlib.ObjectDetector;
import com.venue.hjt.venueclass.R;
import com.venue.hjt.venueclass.utlis.CameraManager;

import org.opencv.core.Scalar;

import java.io.IOException;
import java.util.ArrayList;

public class FaceTrackingActivity extends Activity {
    private ArrayList<ObjectDetector> mObjectDetects;
    private ObjectDetector mFaceDetector;
    private float absoluteFaceSize = 2.2F;
    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;
    private CameraManager cameraManager;
    private CardView cv_face;
    private int i=8;
    String id="";
//    private StudentReceiver newsReceiver;
    EditText tv_Card;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去除title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //去掉Activity上面的状态栏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.activity_face_tracking);
//        newsReceiver = new StudentReceiver();
//        registerReceiver(newsReceiver, filter());
        tv_Card= findViewById(R.id.tv_Card);
        cv_face=findViewById(R.id.cv_face);
        mObjectDetects = new ArrayList<>();
        mFaceDetector = new ObjectDetector(this, R.raw.haarcascade_frontalface_alt, 6, absoluteFaceSize, absoluteFaceSize, new Scalar(255, 0, 0, 255));
        mObjectDetects.add(mFaceDetector);
        mSurfaceView = findViewById(R.id.java_surface_view);
        mSurfaceHolder = mSurfaceView.getHolder();
        id = getIntent().getStringExtra("id");
        // mSurfaceView 不需要自己的缓冲区
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        // mSurfaceView添加回调
        mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) { //SurfaceView创建
                try {
                    cameraManager = new CameraManager(FaceTrackingActivity.this, mObjectDetects , mSurfaceHolder,id);
                    cameraManager.openDriver();
                    cameraManager.startPreview();
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) { //SurfaceView销毁
                holder.removeCallback(this); // Camera is being used after Camera.release() was called
                cameraManager.stopPreview();
                cameraManager.closeDriver();
            }
        });
        Log.e("ATG","场馆"+id);
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (; i >0; i--) {
                    handler1.sendEmptyMessage(1);
                    if(i<=0){
                        break;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                handler1.sendEmptyMessage(2);
            }
        }).start();
    }
    Handler handler1 = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 1) {
//                tv_countdown.setText("还剩" + i + "关闭界面");
            } else {
                finish();
            }
        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
//        unregisterReceiver(newsReceiver);
    }
}